package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilab;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Ciuser;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CilabService;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CiuserService;

/**
 * 跳转前，进行request域的设置
 */
public class jumpToServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Ciuser ciuser = (Ciuser) request.getSession().getAttribute("ciuser");
		
		
		if (ciuser == null || ciuser.getType() == 0) {
			System.out.print("无账户登录或账户等级不够");
			response.sendRedirect("../errorPage/toLoginErrorPage.html");
			return;
		}
		
		String modeString = request.getParameter("mode");
		// 用户
		if ("ciuser".equals(modeString)) {
			String idString = request.getParameter("id");
			int id = Integer.parseInt(idString);
			CiuserService ciuserService = BeanFactory.getBean("ciuserService");
			Ciuser myCiuser = ciuserService.findCiuserById(id);
			request.setAttribute("ciuser", myCiuser);
			
			//请求包含 - 带着request内的ciuser
			request.getRequestDispatcher("cimodify.jsp").include(request, response);
			return;
		}
		// 实验室
		else if ("cilab".equals(modeString)) {
			String idString = request.getParameter("id");
			int id = Integer.parseInt(idString);
			CilabService cilabService = BeanFactory.getBean("cilabService");
			Cilab cilab = cilabService.findCilabById(id);
			request.setAttribute("cilab", cilab);
			//请求包含 - 带着request内的cilab
			request.getRequestDispatcher("cilabmodify.jsp").include(request, response);
		}
		
		
		else if("citext".equals(modeString)) {
			String idString = request.getParameter("id");
			int id = Integer.parseInt(idString);
			CilabService cilabService = BeanFactory.getBean("cilabService");
			Cilab cilab = cilabService.findCilabById(id);
			request.setAttribute("cilab", cilab);
			//请求包含 - 带着request内的cilab
			request.getRequestDispatcher("cilabcontextmodify.jsp").include(request, response);
		}
		
		else if("cilabrecord".equals(modeString)) {
			String idString = request.getParameter("id");
			int id = Integer.parseInt(idString);
			CilabService cilabService = BeanFactory.getBean("cilabService");
			Cilab cilab = cilabService.findCilabById(id);
			request.setAttribute("cilab", cilab);
			//请求包含 - 带着request内的cilab
			request.getRequestDispatcher("cilabrecord.jsp").include(request, response);
		}
		else {
			response.sendError(567, "no mode");
			return;
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
